package com.example.module_mine.login.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.module_mine.login.loginnet.LoginModel
import com.example.module_mine.login.loginnet.LoginRePo
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(private val loginRePo: LoginRePo) : BaseViewModel() {
    val loginfailedlivedata by lazy {
        MutableLiveData<String>()
    }
    val loginsuccesslivedata by lazy {
        MutableLiveData<LoginModel>()
    }
    fun login(username: String, password: String) {
        execute(
            action = loginRePo.login(username, password),
            onFinish = {
                loginsuccesslivedata.value=it.data
//                loginlivedata.value = "登陆成功"
//                SPUtils.getInstance().put("token", "${it.data.token}")
//                //不实时同步，需要在mmkv模式中封装同步异步方法
//                MMKV.defaultMMKV().encode("userid", it.data.id)
//                MMKV.defaultMMKV().encode("token", "${it.data.token}")
//                MMKV.defaultMMKV().encode("username", it.data.username)
//                MMKV.defaultMMKV().encode("password", it.data.password)

                Log.d("sss", "login: " + it.code)
            },
            onError = {
                loginfailedlivedata.value = "登录失败，账号或密码有误"
                Log.d("sss", "login: " + it)
            }
        )
    }

}